package com.cxy.web;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.cxy.pojo.Document;
import com.cxy.pojo.Project;
import com.cxy.pojo.User;
import com.cxy.services.DocumentServices;
import com.cxy.services.ProjectServices;
import com.cxy.util.DateUtil;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

@Results({@Result(location="project!get.jspx",params={"id","${document.project.id}"},name=BaseAction.PROJECT,type="redirectAction"),@Result(name=BaseAction.PDF_RESULT,location="count.pdf",type="stream")})
public class DocumentAction extends BaseAction {
	private static final long serialVersionUID = -991852434286927965L;
	@Resource
	private ProjectServices projectServices;
	@Resource
	private DocumentServices documentServices;
	private Document document;
	private Project project;
	@Override
	public String add() throws Exception {
		String now = DateUtil.getNow();		
		User u = getUserInSession();
		
		document.setCreateTime(now);
		document.setUser(u);
		documentServices.saveOrUpdate(document);
		return PROJECT;
	}
	public String edit() throws Exception {
		document = documentServices.get(document.getId());
		return EDIT;
	}
	public String download() throws Exception {
		response.setContentType(BaseAction.PDF_TYPE);		
		document = documentServices.get(document.getId());
		com.lowagie.text.Document doc = new com.lowagie.text.Document();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PdfWriter.getInstance(doc, baos);
		doc.open();
		
		BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
		Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
		Paragraph pragraph=new Paragraph(document.getContent(),FontChinese);

		doc.add(pragraph);		
		doc.close();
		
		response.setContentLength(baos.size());
//		response.setHeader("Content-Type","application/pdf");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String( document.getTitle().getBytes("gb2312"), "ISO8859-1" )+".pdf");
		OutputStream out = response.getOutputStream();
		baos.writeTo(out);
		return null;
	}
	@Override
	public String get() throws Exception {
		return null;
	}

	@Override
	public String del() throws Exception {
		return null;
	}

	@Override
	public String input() throws Exception {
		project = projectServices.get(document.getProject().getId());
		return INPUT;
	}

	@Override
	public String update() throws Exception {
		documentServices.saveOrUpdate(document);
		return PROJECT;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}
}
